package com.ding.bq;

import java.util.concurrent.ArrayBlockingQueue;

/**
 * @author ding
 * @date 2021/7/18 10:27
 */


public class BlockingQueueDemo {
    public static void main(String[] args) {
        //Collection
        //ArrayList
        //Set
//        test1();
        test2();
    }

    /**
     * 抛出异常
     */
    public static void test1() {
        //队列大小
        ArrayBlockingQueue<Object> blockingQueue = new ArrayBlockingQueue<>(3);
        System.out.println(blockingQueue.add("A"));
        System.out.println(blockingQueue.add("B"));
        System.out.println(blockingQueue.add("C"));
        //java.lang.IllegalStateException: Queue full
//        System.out.println(blockingQueue.add("d"));
        System.out.println("====================");
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());

        //java.util.NoSuchElementException
//        System.out.println(blockingQueue.remove());

    }

    /**
     *有返回值 不会抛出异常
     */
    public static void test2(){
        ArrayBlockingQueue<Object> blockingQueue = new ArrayBlockingQueue<>(3);

        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));
        //不会抛出异常
        System.out.println(blockingQueue.offer("e"));

        System.out.println("================");
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        //返回null不会报错
        System.out.println(blockingQueue.poll());

    }
}
